package org.example.springmybatisdemo.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.example.springmybatisdemo.entity.Dept;
import org.example.springmybatisdemo.entity.PageBean;
import org.example.springmybatisdemo.entity.Result;
import org.example.springmybatisdemo.service.DeptServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Scope("prototype") // 非单例 调用时才创建
@Tag(name = "部门管理")
@RequestMapping("/api/depts") // 基础路径
@RestController
public class DeptController {
//    构造器
    public DeptController() {
        System.out.println("DeptController被创建了");
    }

    @Resource(name = "deptServiceImpl")
    private DeptServiceImpl deptService;
    Logger log = LoggerFactory.getLogger("com.foo");

    //    获取部门列表
    @Operation(summary = "部门列表（无分页）---看这个，这个是正常示例")
    @ApiResponse(responseCode = "200", description = "查询成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)))
    @GetMapping("/deptlist")
    public Result<List<Dept>> list() {
        //        1. 调用service获取数据
        List<Dept> deptlist = deptService.deptlist();
        log.info(deptlist.toString());
        //        2. 响应数据
        return Result.success(deptlist) ;
    }

    //    删除部门
    @Operation(summary = "删除部门")
    @DeleteMapping("/deletedept")
    public  Result<Void> delete(@RequestParam Integer id) {

        try {
            deptService.delete(id);
            return Result.success();
        } catch (RuntimeException e) {
            return Result.fail(e.getMessage(), 400);
        }
    }

    //新增部门
    @Operation(summary = "新增部门")
    @PostMapping("/adddept") // @RequestBody用来接受json格式数据
    public  Result<Void> add(@RequestBody Dept dept) {
        deptService.add(dept);
        return Result.success();
    }

    //回显（详情）
    @Operation(summary = "部门详情")
    @GetMapping("/deptdetail/{id}")
    public Result detail(@PathVariable Integer id) {
        Dept dept = deptService.detail(id);
        return Result.success(dept);
    }

    //    编辑部门
    @Operation(summary = "编辑部门")
    @PostMapping("/editdept")
    public Result<Dept> edit(@RequestBody Dept dept) {
        deptService.edit(dept);
        return Result.success();
    }

    @Operation(summary = "原生分页部门列表")
    //    原生分页查询
    @GetMapping("/deptlistpage")
    public Result<PageBean> listpage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "5") Integer pageSize
            ) {
        //        1. 调用service获取数据
        List<Dept> deptlist = deptService.deptlist();
        log.info(deptlist.toString());
        //        2. 响应数据
        PageBean pagebean = deptService.pagelist(page, pageSize);
        return Result.success(pagebean) ;
    }

    //    分页插件
    @Operation(summary = "分页部门列表-插件方式")
    @GetMapping("/deptlistpluginpage")
    public Result<PageBean> listpluginpage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "5") Integer pageSize
    ) {
        //        1. 调用service获取数据
        List<Dept> deptlist = deptService.deptlist();
        log.info(deptlist.toString());
        //        2. 响应数据
        PageBean pagebean = deptService.pagePluginlist(page, pageSize);
        return Result.success(pagebean) ;
    }

    @GetMapping("/")
    public String index() {
        return "Hello from controller!9999";
    }

    @GetMapping("/testget")
    public String greet(@RequestParam String name) {
        return "Hello, " + name;
    }

    @PostMapping("/testpost")
    public String sayHelloPost(@RequestBody String name) {
        return "Hello, " + name;
    }

    @GetMapping("/geterror")
    public Result<String> getError() {
//        模拟错误
//        int i = 1/0;
        return Result.success("成功");
    }
}
